﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Spring;
using Cn.Cellula.Biz.Port;
using Cn.Cellula.Model;
using System.Drawing;
using System.Collections.Generic;
using Cn.Cellula.Client.ControlHelper;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Model.Content;
using Cn.Cellula.Client;
namespace Cn.Cellula.Website.Admin.Content.Vote {
    public partial class List : PageSupport {
        IVoteBiz biz = BizFactory.Get<IVoteBiz>();
        String VoteKey = null;
        protected void Page_Load(object sender, EventArgs e) {
            VoteKey = Request.QueryString["t"];
            if (!Page.IsPostBack) {
                LoadTypeList();
            }
        }

        private void LoadTypeList() {
            IList<VVoteEditListEO> list = biz.LoadAllVote(this.OwnerKey);
            this.gvlist.DataSource = list;
            this.gvlist.DataBind();
            if (String.IsNullOrEmpty(VoteKey)) {
                if (list != null && list.Count > 0) {
                    LoadItemList(list[0].VoteKey);
                    GvHelper.GvSelected(ref this.gvlist, list[0].VoteKey, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
                }
            } else {
                LoadItemList(VoteKey);
                GvHelper.GvSelected(ref this.gvlist, VoteKey, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            }
        }


        private void LoadItemList(String rVoteKey) {
            this.hid_SelectedVoteKey.Value = rVoteKey;
            VVoteEditListEO teo = biz.Load<VVoteEditListEO>(rVoteKey);
            if (teo.IsOpened == 1) {
                this.btn_value_add.Enabled = false;
                this.btn_value_delete.Enabled = false;
                this.btn_value_update.Enabled = false;
                this.btn_type_delete.Enabled = false;
            } else {
                this.btn_value_add.Enabled = true;
                this.btn_value_delete.Enabled = true;
                this.btn_value_update.Enabled = true;
                this.btn_type_delete.Enabled = true;
            }
            this.gvItemList.DataSource = biz.LoadItemsByVote(rVoteKey);
            this.gvItemList.DataBind();
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvlist_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            gvlist.PageIndex = e.NewPageIndex;
            LoadTypeList();
        }

        protected void gvlist_RowDataBound(object sender, GridViewRowEventArgs e) {
            if (e.Row.RowType == DataControlRowType.DataRow) {
                //String key = gvlist.DataKeys[e.Row.RowIndex].Values[0].ToString();
                //string url = "Admin/Content/Code/_CodeList.aspx";
                //e.Row.Attributes.Add("url", url + "?ID=" + key);
                //e.Row.Attributes.Add("onclick", "return myUtil.onGridViewRowSelected(this,event,'cntrlist_container');");
            }
        }

        protected void btn_type_add_Click(object sender, EventArgs e) {
            Response.Redirect("EditVote.aspx");
        }

        protected void btn_type_update_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvlist, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys.Count > 0) {
                Response.Redirect("EditVote.aspx?ID=" + keys[0] + "");
            }
        }

        protected void btn_type_delete_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvlist, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            biz.DeleteVote(keys,this.OwnerKey);
            BizFactory.Get<ILogBiz>().LogDelete("投票活动", keys.Count, this.Actor, Request);
            LoadTypeList();
        }

        protected void btn_value_add_Click(object sender, EventArgs e) {
            Response.Redirect("EditItem.aspx?t=" + this.hid_SelectedVoteKey.Value);
        }

        protected void btn_value_update_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvItemList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys.Count > 0) {
                Response.Redirect("EditItem.aspx?ID=" + keys[0] + "");
            }
        }

        protected void btn_value_delete_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvItemList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            biz.DeleteVoteItem(keys);
            BizFactory.Get<ILogBiz>().LogDelete("投票项", keys.Count, this.Actor, Request);
            this.LoadItemList(this.hid_SelectedVoteKey.Value);
        }

        protected void gvlist_RowCommand(object sender, GridViewCommandEventArgs e) {
            if (e.CommandName == "CheckSubList") {
                String typeKey = e.CommandArgument.ToString();
                this.hid_SelectedVoteKey.Value = typeKey;
                this.LoadItemList(typeKey);
                GvHelper.GvSelected(ref this.gvlist, typeKey, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            }
        }

        protected void btn_Open_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvlist, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                biz.OpenVote(keys);
                BizFactory.Get<ILogBiz>().Info("开启投票活动", this.Actor, Request);
                LoadTypeList();
            }
        }

        protected void btn_Close_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvlist, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                biz.CloseVote(keys);
                BizFactory.Get<ILogBiz>().Info("关闭投票活动", this.Actor, Request);
                LoadTypeList();
            }
        }
    }
}
